package org.zxp.thread.completablefuture;

import java.util.concurrent.CompletableFuture;

/**
 * @program: zxp-thread-test
 * @description:
 * @author: X-Pacific zhang
 * @create: 2020-08-05 14:20
 **/
public class Test {
    public static void main(String[] args) throws Exception {
        // 创建异步执行任务:
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(Test::fetchPrice);
        // 如果执行成功:
        cf.thenAccept((result) -> {
            System.out.println("price: " + result);
        });
        // 如果执行异常:
        cf.exceptionally((e) -> {
            e.printStackTrace();
            return null;
        });


        //// 创建异步执行任务:
        //CompletableFuture cf = CompletableFuture.runAsync(() -> {
        //    try {
        //        Thread.sleep(1000);
        //    } catch (InterruptedException e) {
        //    }
        //    System.out.println("inner excute");
        //    //throw new NullPointerException("222");
        //});
        //// 如果执行成功:
        //cf.thenAccept((result) -> {
        //    System.out.println("price: " + result);
        //});
        //// 如果执行异常:
        //cf.exceptionally((e) -> {
        //    System.out.println("error:"+e);
        //    return null;
        //});

        System.out.println("main over");
        Thread.sleep(99999);
    }

    static String fetchPrice() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        throw new NullPointerException("222");
        //return "zxp";
    }

}
